<?php
class Leonardo extends MY_SecuredController {

	public function __construct() {
		parent::MY_SecuredController();

		$this->requireRole('User');

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');

		$this->lang->load('home');
		$this->lang->load('profile');
	}

	public function import($civlid) {
		Doctrine::debug(true);

		$this->_preRender($civlid);

		$person = PersonModel::getPerson($civlid);
		$pilot = $person->Pilot;

		if (!$this->input->post('action')) {
			$this->redirect('profile/pilot/edit/'.$civlid);
			return;
		}

		$phpbbUser = Doctrine::getTable('PhpbbUserModel')->findOneByUsername($this->input->post('leonardo_user'));

		if ($phpbbUser == null) {
            $this->redirect('profile/pilot/edit/'.$civlid, 'PhpBB user not found - contact Administrator', true);
            return;
		}

		if (md5($this->input->post('leonardo_pass')) != $phpbbUser->user_password) {
			$this->redirect('profile/pilot/edit/'.$civlid, 'Wrong CND username or password - try again', true);
			return;
		}

		$leonardoPilot = Doctrine::getTable('LeonardoPilotModel')->findOneByPilotID($phpbbUser->user_id);
		//echo '<pre>'; var_dump($leonardoPilot->toArray()); echo '</pre>';
		log_message('debug', '>>> Profile/Leonardo - leonardoPilot->pilotid = '.$leonardoPilot->pilotid);
		log_message('debug', '>>> Profile/Leonardo - leonardoPilot->sponsor = '.$leonardoPilot->sponsor);

		if ($leonardoPilot == null) {
            $this->redirect('profile/pilot/edit/'.$civlid, 'Leonardo/CND user not found - contact Administrator', true);
            return;
		}

		//echo 'Ok, user_id = '.$leonardoPilot->firstname.'<br/>';
        $pilot->helmet = $leonardoPilot->helmet;
        $pilot->harness = $leonardoPilot->harness;
        $pilot->gps = $leonardoPilot->gps;
        $pilot->vario = $leonardoPilot->vario;
        $pilot->rescue = $leonardoPilot->reserve;
        $pilot->sponsor = $leonardoPilot->sponsor;
        $pilot->comments = $leonardoPilot->personalwebpage;
        $pilot->leonardo_id = $leonardoPilot->pilotid;
		
		$data['person'] = $person;
		$data['pilot'] = $pilot;

		$this->render('profile/pilot', $data);
	}
}
